﻿using MetroDataApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class DeletePointsController : ApiController
    {
        // GET api/deletepoints
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int prjId, string date, string monitorType = "", string location = "")
        {
            using (var db = new MetroDataDataContext())
            {
                if (location == null || location == "")
                {
                    monitorType = "";
                    location = "";
                }
                var dateTime = DateTime.Parse(date);
                var ptNames = new List<string>();
                if (monitorType == "")
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId).Select(p => p.Name).ToList();
                }
                else
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.Location == location &&
                        p.MonitorType == monitorType).Select(p => p.Name).ToList();
                }

                var pts = from n in ptNames
                          join
                              p in db.MonitorPoints.Where(t => t.PrjId == prjId && t.DateTime.Value == dateTime) on n equals p.Name
                          into gs
                          from gi in gs.DefaultIfEmpty()
                          select gi;
                db.MonitorPoints.DeleteAllOnSubmit(pts);
                db.SubmitChanges();
                return "OK";
            }
        }


        // DELETE api/deletemonitorrecord/5
        public void Delete(int prjId, string date, string monitorType = "", string location = "")
        {
            using (var db = new MetroDataDataContext())
            {
                if (location == null || location == "")
                {
                    monitorType = "";
                    location = "";
                }
                var dateTime = DateTime.Parse(date);
                var ptNames = new List<string>();
                if (monitorType == "")
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId).Select(p => p.Name).ToList();
                }
                else
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.Location == location &&
                        p.MonitorType == monitorType).Select(p => p.Name).ToList();
                }

                var pts = from n in ptNames
                          join
                              p in db.MonitorPoints.Where(t => t.PrjId == prjId && t.DateTime.Value == dateTime) on n equals p.Name
                          into gs
                          from gi in gs.DefaultIfEmpty()
                          select gi;
                db.MonitorPoints.DeleteAllOnSubmit(pts);
                db.SubmitChanges();
            }
        }
    }
}
